package com.example.easyutils.watermark;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.musicpage.MusicMain;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class Vendor extends BaseActivity {
    ZLoadingDialog dialog = new ZLoadingDialog(Vendor.this);//加载页面动画
    private EditText editText;
    private Map<String, String> videoUrl;
    private final static int SUCCESS_CODE = 001,ERROR_CODE = 002;
    private String vendorUrl,vendorClearWatermark = null;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            dialog.cancel();
            switch (msg.what)
            {
                case SUCCESS_CODE:
                    String url = null;
                    if(videoUrl.get(vendorClearWatermark)!="") url = videoUrl.get(vendorClearWatermark);
                    Log.d("testst",videoUrl+"");
                    Log.d("testst22",url+"");
                    Log.d("111teststs",vendorClearWatermark+"");
                    if(url==null){
                        Toast.makeText(Vendor.this,"视频路径出错啦！",Toast.LENGTH_LONG).show();
                        return;
                    }
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);

//                    "http://baike.baidu.com/view/753813.htm"为你要转到的网站。
                    break;
                case ERROR_CODE:
                    Toast.makeText(Vendor.this,"出错啦！",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vendor_layout);
        editText = (EditText)findViewById(R.id.editText);
        Intent intent = getIntent();
        String vendorStr = intent.getStringExtra("Vendor");
        Log.d("Vendor",vendorStr+"");
        init(vendorStr);

    }

    public void init(String vendorStr){
//        "抖音","快手","皮皮虾","微视","秒拍","火山","看点"
        switch (vendorStr){
            case "抖音":
                vendorUrl = "http://api.63code.com/douyin/api.php";
                editText.setHint("请输入抖音视频地址");
                vendorClearWatermark = "video_url";
                break;
            case "快手":
                vendorUrl = "http://api.63code.com/kuaishou/api.php";
                editText.setHint("请输入快手视频地址");
                vendorClearWatermark = "mp4url";
                break;
            case "皮皮虾":
                vendorUrl = "http://api.63code.com/pipixia/api.php";
                editText.setHint("请输入皮皮虾视频地址");
                vendorClearWatermark = "cn_url";
                break;
            case "微视":
                vendorUrl = "http://api.63code.com/weishi/api.php";
                editText.setHint("请输入微视视频地址");
                vendorClearWatermark = "url";
                break;
            case "秒拍":
                vendorUrl = "http://api.63code.com/mp/api.php";
                editText.setHint("请输入秒拍视频地址");
                vendorClearWatermark = "play_url";
                break;
            case "火山":
                vendorUrl = "http://api.63code.com/huoshan/api.php";
                editText.setHint("请输入火山视频地址");
                vendorClearWatermark = "video_url";
                break;
            case "看点":
                vendorUrl = "http://api.63code.com/kd/api.php";
                editText.setHint("请输入看点视频地址");
                vendorClearWatermark = "video_url";
                break;
        }
    }

    public void commit(View v){//确定
        dialog.setLoadingBuilder(Z_TYPE.LEAF_ROTATE)//设置类型
                .setLoadingColor(Color.RED)//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.GRAY)  // 设置字体颜色
                .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(Color.parseColor("#CC111111")) // 设置背景色，默认白色
                .show();
//        http://v.douyin.com/5w5JwL/
//        https://v.douyin.com/JfSrLuP/
        String url = editText.getText().toString();
        Log.d("url",url);
        if(url.equals("")){
            Toast.makeText(Vendor.this,"视频路径不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        HttpGetRequest.sendOkHttpGetRequst(vendorUrl+"?url="+url,
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("ERRORCODE",e+"");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String dataMap = response.body().string();

                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, String>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,String> dataDetail = gson.fromJson(dataMap,new TypeToken<Map<String,String>>(){

                        }.getType());
                        Log.d("SUCCESS1",dataMap);
//                        Log.d("SUCCESS",dataDetail.get("video_url")+"");
                        videoUrl = dataDetail;
                        handler.sendEmptyMessage(SUCCESS_CODE);

                    }
                });
    }
}
